/**
 * Created by tdzl2003 on 9/10/16.
 */

import React, { PropTypes } from 'react';
import FormModal from '../FormModal';
import {Form, Input} from 'antd';
import {changeMyPwd} from '../../logics/auth';

@Form.create()
export default class ChangePwdModal extends React.Component {
  onReturn = ev => {
    this.refs.form.submit();
  };
  fields = {
    oldPwd: {
      rules: [
        { required: true, message: '请输入您的旧密码' },
      ],
    },
    newPwd: {
      rules: [
        { required: true, message: '请输入新密码' },
      ],
    },
    newPwd1: {
      rules: [
        { required: true, message: '再次输入新密码' },
        {
          validator: (rule, value, callback) => {
            const {form} = this.props;
            if (value !== form.getFieldValue('newPwd')) {
              callback('两次密码输入不一致！');
              return;
            }
            callback();
          }
        }
      ],
    },
  };
  render() {
    const { fields } = this;
    const { getFieldProps } = this.props.form;
    return (
      <FormModal
        ref="form"
        title="修改密码"
        okTitle="确 定"
        onSubmit={changeMyPwd}
        {...this.props}
      >
        <Form.Item label="请输入您的旧密码">
          <Input {...getFieldProps('oldPwd', fields.oldPwd)} type="password" onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="请输入新密码">
          <Input {...getFieldProps('newPwd', fields.newPwd)} type="password" onPressEnter={this.onReturn} />
        </Form.Item>
        <Form.Item label="再次输入新密码">
          <Input {...getFieldProps('newPwd1', fields.newPwd1)} type="password" onPressEnter={this.onReturn} />
        </Form.Item>
      </FormModal>
    );
  }
}
